package com.pig4cloud.pig.yu.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.yu.entity.CarBrandEntity;
import com.pig4cloud.pig.yu.service.CarBrandService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * car_brand
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/carBrand" )
@Tag(description = "carBrand" , name = "car_brand管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class CarBrandController {

    private final  CarBrandService carBrandService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param carBrand car_brand
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('yu_carBrand_view')" )
    public R getCarBrandPage(@ParameterObject Page page, @ParameterObject CarBrandEntity carBrand) {
        LambdaQueryWrapper<CarBrandEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StrUtil.isNotBlank(carBrand.getBrandName()),CarBrandEntity::getBrandName,carBrand.getBrandName());
		wrapper.orderByDesc(CarBrandEntity::getBrandId, CarBrandEntity::getBrandId);
        return R.ok(carBrandService.page(page, wrapper));
    }


    /**
     * 通过id查询car_brand
     * @param brandId id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{brandId}" )
    @PreAuthorize("@pms.hasPermission('yu_carBrand_view')" )
    public R getById(@PathVariable("brandId" ) Long brandId) {
        return R.ok(carBrandService.getById(brandId));
    }

    /**
     * 新增car_brand
     * @param carBrand car_brand
     * @return R
     */
    @Operation(summary = "新增car_brand" , description = "新增car_brand" )
    @SysLog("新增car_brand" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('yu_carBrand_add')" )
    public R save(@RequestBody CarBrandEntity carBrand) {
		if (StrUtil.isBlank(carBrand.getBrandName())) {
			return R.failed("品牌名称不能为空");
		}
		if (carBrandService.checkBrandName(carBrand.getBrandName())) {
			return R.failed("品牌名称已存在");
		}
		carBrand.setDelFlag(0);
        return R.ok(carBrandService.save(carBrand));
    }

    /**
     * 修改car_brand
     * @param carBrand car_brand
     * @return R
     */
    @Operation(summary = "修改car_brand" , description = "修改car_brand" )
    @SysLog("修改car_brand" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('yu_carBrand_edit')" )
    public R updateById(@RequestBody CarBrandEntity carBrand) {
		if (StrUtil.isBlank(carBrand.getBrandName())) {
			return R.failed("品牌名称不能为空");
		}
		if (carBrandService.checkBrandName(carBrand.getBrandName()) &&
				!Objects.equals(carBrandService.getByBrandName(carBrand.getBrandName()).getBrandId(), carBrand.getBrandId())) {
			return R.failed("品牌名称已存在");
		}
        return R.ok(carBrandService.updateById(carBrand));
    }

    /**
     * 通过id删除car_brand
     * @param ids brandId列表
     * @return R
     */
    @Operation(summary = "通过id删除car_brand" , description = "通过id删除car_brand" )
    @SysLog("通过id删除car_brand" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('yu_carBrand_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(carBrandService.removeBatchByIds(CollUtil.toList(ids)));
    }
}